/* eslint-disable */

// 【注意：】 请看这里！！！
// 这里是demo案例，不要在任何模块使用，仅单纯提供参考

// 这是服务的基础地址，本项目是薪酬管理系统，这里就是薪酬的服务地址。用于后端到网关转发，具体地址配置在环境变量里面了
// 所有的薪酬的api接口都要带上这个服务地址前缀。所以在接口的url前面都要加上这个前缀
// const serverName = process.env.VUE_APP_API_SERVICE_URL

import Mock from 'mockjs'

Mock.Random.extend({
  pickOneFromArray: function (array) {
    // 从数组中随机选择一个元素
    return array[Math.floor(Math.random() * array.length)]
  }
})

// 模拟数据
let arr = []
for (let i = 0; i < 52; i++) {
  const { phone } = Mock.mock({ 'phone|1': /^1(3|4|5|6|7|8|9)\d{9}$/ })
  arr.push({
    id: Mock.mock('@id'),
    name: Mock.mock('@cname'),
    phone,
    age: Mock.Random.integer(18, 60),
    sex: Mock.Random.pickOneFromArray([1, 2, 3]),
    love: Mock.Random.pickOneFromArray(['lq', 'zq', 'ymq']),
    entryDate: Mock.mock('@date'),
    address: Mock.mock('@county(true)'),
    state: Mock.Random.pickOneFromArray([1, 2]),
    remark: Mock.mock('@cparagraph')
  })
}

export const getListUserApi = (params) => {
  console.log('列表查询==>前端提交的数据', params)
  const { pageNum = 1, pageSize = 5, name, phone, sex,age,state } = params
  const startIndex = (pageNum - 1) * pageSize
  const endIndex = startIndex * 1 + pageSize * 1
  let list = [...arr]
  if (name) {
    list = list.filter((item) => item.name.includes(name))
  }
  if (phone) {
    list = list.filter((item) => item.phone * 1 === phone * 1)
  }
  if (sex) {
    list = list.filter((item) => item.sex * 1 === sex * 1)
  }
  if (age) {
    list = list.filter((item) => item.age * 1 === age * 1)
  }
  if (state) {
    list = list.filter((item) => item.state * 1 === state * 1)
  }
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        total: list.length,
        rows: list.slice(startIndex, endIndex)
      })
    }, 500)
  })
}

// 思路： 将原来的mockjscrud逻辑拷贝一下，然后在封装的mock crud工具函数
export const delUserApi = (data) => {
  console.log('删除==>前端提交的参数', data)
  const ids = data.split(',')
  ids.forEach((value) => {
    const index = arr.findIndex((item) => item.id === value )
    console.log('🚀 ~ ids.forEach ~ index:', index)
    arr.splice(index, 1)
  })
  return Promise.resolve({ success: true })
}

export const infoUserApi = (data) => {
  console.log('详情==>前端提交的数据', data)
  let item = arr.find((item) => item.id === data.id)
  if (item) {
    return Promise.resolve({ success: true, data: item })
  } else {
    return Promise.resolve({ success: false, message: '没有找到该数据' })
  }
}

export const createUserApi = (data) => {
  console.log('添加==>前端提交的数据', data)
  let row = { ...data, id: Mock.mock('@id') }
  arr.push(row)
  return Promise.resolve({ data: row, success: true, message: '添加成功' })
}

export const updateUserApi = (data) => {
  console.log('修改==>前端提交的数据', data)
  if (!data.id) return { success: false, message: '请提交ID' }
  arr = arr.map((item) => {
    if (item.id === data.id) {
      return { ...item, ...JSON.parse(JSON.stringify(data)) }
    } else {
      return item
    }
  })
  return Promise.resolve({ success: true, message: '修改成功' })
}
